# from selenium import webdriver
# from selenium.webdriver.chrome.options import Options
# from selenium.webdriver.common.by import By
# from selenium.webdriver.common.action_chains import ActionChains
# from selenium.webdriver.support.ui import WebDriverWait
# import  json
# import logging
# from flask import Flask,request
# from selenium.webdriver.support import expected_conditions as EC
# import random
# from time import sleep
#
# class Meituan:
#     def __init__(self):
#         self.getWdrforLiunx()
#
#
#     def getWdrforLiunx(self):
#         chrome_options = Options()
#         # chrome_options.add_argument("--headless")  # 无界面模式
#         # chrome_options.add_argument("--disable-gpu")  # 无界面模式
#         chrome_options.add_argument("--window-size=1920,1080")
#         # chrome_options.add_argument('--no-sandbox')
#         # chrome_options.add_argument('--ignore-certificate-errors')
#         # chrome_options.add_argument("--disable-extensions")
#         # chrome_options.add_argument("--disable-software-rasterizer")
#         # chrome_options.add_argument('--allow-running-insecure-content')
#         # chrome_options.add_argument("blink-settings=imagesEnabled=false")
#
#         chrome_options.add_argument('--disable-infobars')
#         chrome_options.add_experimental_option('useAutomationExtension', False)
#         chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
#
#         chrome_options.add_argument("--disable-blink-features=AutomationControlled")
#
#
#         # 创建DesiredCapabilities对象
#
#         # 初始化WebDriver
#         wdr = webdriver.Remote(command_executor='http://172.16.0.252:4444/wd/hub', options=chrome_options)
#         wdr.implicitly_wait(5)
#         wdr.get('https://e.waimai.meituan.com/new_fe/login_gw#/login')
#         self.wdr = wdr
#         print(f'{self.wdr.current_url}')
#     def getWdrforWinow(self):
#         #初始化页面
#         chrome_options = Options()
#         chrome_options.add_experimental_option('useAutomationExtension', False)
#         chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
#         chrome_options.add_argument("--disable-blink-features=AutomationControlled")
#
#         wdr = webdriver.Chrome(options=chrome_options)
#         wdr.implicitly_wait(5)
#
#         wdr.get('https://e.waimai.meituan.com/new_fe/login_gw#/login')
#         self.wdr = wdr
#         print(f'前URL为{self.wdr.current_url}')
#     def login_by_code(self,phone,code):
#         self.wdr.refresh();
#         print(f'{self.wdr.current_url}')
#         #选择验证码登录选项
#         self.wdr.find_element(By.XPATH, '//*[@id="epLoginComponent"]/div[1]/div[2]').click()
#         #点击我已阅读条款
#         self.wdr.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/div/div/div[2]/div[4]/div/div/label').click()
#         #输入手机号
#         self.wdr.find_element(By.XPATH, '//*[@id="phone"]').send_keys(phone)
#         #输入验证码
#         self.wdr.find_element(By.XPATH,'//*[@id="epLoginComponent"]/div[2]/div[2]/div/input').send_keys(code)
#         self.wdr.find_element(By.XPATH,'//*[@id="epLoginComponent"]/div[2]/button').click();
#         #开始检测有没有登录成功
#         if self.login_success():
#             return None;
#         #判断没有有出现错误信息
#         err = self.code_err_show()
#         if err != None:
#             return err;
#         else:
#             return '未知异常'
#
#
#     def login_by_password(self,username,password):
#         self.wdr.refresh();
#         print(f'{self.wdr.current_url}')
#         #点击我已阅读条款
#         self.wdr.find_element(By.XPATH,'//*[@id="epLoginComponent"]/form/div[4]/div/div/label').click();
#         #输入手机号
#         self.wdr.find_element(By.XPATH, '//*[@id="login"]').send_keys(username)
#         #输入密码
#         self.wdr.find_element(By.XPATH,'//*[@id="password"]').send_keys(password)
#         #点击登录
#         self.wdr.find_element(By.XPATH, '//*[@id="epLoginComponent"]/form/button').click();
#         #开始检测有没有登录成功
#         if self.login_success():
#             return None;
#         #开始检测有没有出现错误信息
#         errMsg = self.err_messge_is_show()
#         if errMsg != None:
#             return errMsg
#
#         if self.show_slider() == True:
#             print('开始尝试移动滑块')
#             self.move_slider()
#         #开始检测有没有出现错误信息
#         errMsg = self.err_messge_is_show()
#         if errMsg != None:
#             return errMsg
#         #开始检测有没有登录成功
#         if self.login_success():
#             return None;
#         else:
#             return '未知异常'
#
#     def send_code(self,mobile):
#         self.wdr.refresh();
#         print(f'{self.wdr.current_url}')
#         #选择验证码登录选项
#         self.wdr.find_element(By.XPATH, '//*[@id="epLoginComponent"]/div[1]/div[2]').click()
#         #点击我已阅读条款
#         self.wdr.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/div/div/div[2]/div[4]/div/div/label').click()
#         #输入手机号
#         self.wdr.find_element(By.XPATH, '//*[@id="phone"]').send_keys(mobile)
#         #点击发送验证码
#         self.wdr.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div').click()
#         #判断验证码是否发送成功
#         if self.code_is_send_success():
#             return None
#         #判断没有有出现错误信息
#         err = self.code_err_show()
#         if err != None:
#             return err;
#         #判断是否出现了滑块
#         if self.is_code_slider_show():
#             self.move_slider();
#         #再次判断验证码是否发送成功
#         if self.code_is_send_success():
#             return None
#         else:
#             return '未知异常'
#
#
#
#
#     #开始判断发送验证码时没有没出现错误信息
#     def code_err_show(self):
#         try:
#             element = WebDriverWait(self.wdr, 2).until(
#                 EC.visibility_of_element_located((By.CLASS_NAME, 'ep-message_content'))
#             )
#             print(f'出现错误信息了')
#             return element.text
#         except Exception as e:
#             logging.exception(e)
#             print(f'没有出现错误信息')
#             return None;
#     def is_code_slider_show(self):
#         print(f'开始判断没有没出现滑块')
#         try:
#             element = WebDriverWait(self.wdr, 2).until(  # 判断是否弹出了滑块
#                 EC.element_to_be_clickable((By.ID, 'ep-yoda-container'))
#             )
#             print(f'出现滑块了')
#             return True;
#         except Exception as e:
#             logging.exception(e)
#             print(f'没有出现滑块')
#             return False;
#     def move_slider(self):
#         print(f'开始移动滑块')
#         # 滑块元素
#         slider = self.wdr.find_element(By.XPATH, '//*[@id="yodaBox"]')
#         sliderbj = self.wdr.find_element(By.XPATH, '//*[@id="yodaBoxWrapper"]/label')
#         length = sliderbj.size['width'] + slider.size['width'] + 4
#         ActionChains(self.wdr).click_and_hold(slider).perform()
#         x = 0
#         while (x < length):
#             step = random.randint(55, 60)
#             x = x + step;
#             print(f'开始移动{step}步数')
#             ActionChains(self.wdr).move_by_offset(step, 0).perform()
#         ActionChains(self.wdr).release().perform();
#         sleep(2)
#         try:
#             self.wdr.find_element(By.XPATH, '//*[@id="yodaBox"]')
#         except Exception as e:
#             logging.exception(e)
#             print('滑块滑动成功')
#             return
#         print('滑块滑动失败再次尝试')
#         self.move_slider()
#     def code_is_send_success(self):
#         print(f"开始判断验证码是否发送成功")
#         try:
#             element = WebDriverWait(self.wdr, 2).until(
#                 EC.text_to_be_present_in_element((By.XPATH, '//*[@id="epLoginComponent"]/div[2]/div[2]/div/div'), '重新获取')
#             )
#             print(f'验证码发送成功了')
#             return True
#         except Exception as e:
#             logging.exception(e)
#             print(f'验证码没有发送成功了可能出现了滑块')
#             return False;
#
#     def login_success(self):
#         sleep(1)
#         print(f'前URL为{self.wdr.current_url}')
#        # print(f'当前页面是否包含region_id{self.wdr.current_url.find('region_id')}')
#         if self.wdr.current_url.find('region_id') != -1:#如何包含则说明登录成功
#             print(f'登录成功,一切顺利。。。。。。。。。')
#             print(f'强制等待3秒,让selemin获取cookie信息')
#             sleep(3)
#             return True
#         else:
#             print(f'还是原来的地址并没有跳转,说明可能账号密码错误也可能出现了滑块')
#             return False
#         # 判断是否出现了提示信息
#     def err_messge_is_show(self):
#         print(f'开始判断有无出现提示信息')
#         try:
#             element = WebDriverWait(self.wdr, 2).until(  # 判断是否弹出了错误信息
#                 EC.visibility_of_element_located((By.CLASS_NAME, 'ep-message_content'))
#             )
#             print(f'提示信息出现了内容为{element.text}')
#             return element.text;
#         except Exception as e:
#             logging.exception(e)
#             print(f'提示信息没有出现')
#             return None
#
#     def show_slider(self):
#         print(f'开始判断是否出现了滑块')
#         try:
#             element = WebDriverWait(self.wdr, 2).until(  # 判断是否弹出了滑块
#                 EC.visibility_of_element_located((By.ID, 'ep-yoda-container'))
#             )
#             print('出现了滑块')
#             return True;
#         except Exception as e:
#             logging.exception(e)
#             print('没有出现了滑块')
#             return False;
#
#     def getStoreName(self):
#         return "null";
#     def getCookie(self):
#         cookies = self.wdr.get_cookies();
#         # 将cookie转换为分号表示的格式
#         cookie_str = self.getCookieStr(cookies)
#         print(f'订单cookie为{cookie_str}')
#         ecookie_str = "null";
#         try:
#             self.wdr.get('https://waimaieapp.meituan.com/gw/customer/comment/list')
#             sleep(2)
#             ecookie = self.wdr.get_cookies();
#             ecookie_str = self.getCookieStr(ecookie)
#             print(f'评价cookie为{ecookie_str}')
#         except Exception as e:
#             logging.exception(e)
#         return {'cookie':cookies,'cookieStr':cookie_str,'ev_cookie':ecookie_str}
#
#     def getCookieStr(self,cookies):
#         cookie_str = ''
#         for cookie in cookies:
#             cookie_str += cookie['name'] + '=' + cookie['value'] + ';'
#         return cookie_str;
#     def forcequit(self):
#         try:
#             print(f'开始关闭浏览器')
#             self.wdr.quit();
#             print(f'浏览器已经退出')
#         except Exception as e:
#             logging.exception(e)
#             print(f'退出异常')
#
#
#
#
# app = Flask(__name__)
#
# @app.route('/sendcode')
# def sendcodes():
#     result = Result()
#     try:
#         meituan =  Meituan()
#         phone = request.args.get('mobile')
#         errMsg = meituan.send_code(phone)
#         if errMsg != None:
#               result.setCode(500)
#               result.setMessage(errMsg)
#               print(result.toJson())
#               meituan.forcequit()
#               return result.toJson()
#         result.setCode(0)
#         result.setMessage('success')
#         meituan.forcequit()
#         return result.toJson()
#     except Exception as e:
#         logging.exception(e)
#         meituan.forcequit()
#         result.setCode(500)
#         result.setMessage('系统异常，请联系管理员')
#         return result.toJson()
#
# @app.route('/loginBycode')
# def loginbycode():
#     result = Result()
#     try:
#         meituan =  Meituan()
#         phone = request.args.get('phone')
#         code = request.args.get('code')
#         errMsg = meituan.login_by_code(phone,code)
#         if errMsg != None:
#               result.setCode(500)
#               result.setMessage(errMsg)
#               print(result.toJson())
#               meituan.forcequit()
#               return result.toJson()
#         storeName = meituan.getStoreName()
#         cookie = meituan.getCookie();
#         result.setCode(0)
#         result.setMessage('success')
#         result.setData({'storeName':storeName,'cookieInfo':cookie})
#         meituan.forcequit()
#         return result.toJson()
#     except Exception as e:
#         logging.exception(e)
#         meituan.forcequit()
#         result.setCode(500)
#         result.setMessage('系统异常，请联系管理员')
#         return result.toJson()
#
#
# @app.route('/logbypassowrd')
# def usernamepassword():
#
#     result = Result()
#     try:
#         meituan = Meituan()
#         username = request.args.get('username')
#         password = request.args.get('password');
#         errMsg = meituan.login_by_password(username,password)
#         if errMsg != None:
#               result.setCode(500)
#               result.setMessage(errMsg)
#               print(result.toJson())
#               meituan.forcequit()
#               return result.toJson()
#
#         storeName = meituan.getStoreName()
#         cookie = meituan.getCookie();
#         result.setCode(0)
#         result.setMessage('success')
#         result.setData({'storeName':storeName,'cookieInfo':cookie})
#         meituan.forcequit()
#         return result.toJson()
#         #return {'code':0,'msg':'success','storeName':storeName,'cookieInfo':cookie}
#     except Exception as e:
#         logging.exception(e)
#         meituan.forcequit()
#         result.setCode(500)
#         result.setMessage('系统异常，请联系管理员')
#         return result.toJson()
#
#
# class Result:
#     def __init__(self):
#         pass
#     def toJson(self):
#         return json.dumps(self.__dict__,ensure_ascii=False)
#     def setCode(self,code):
#         self.code = code;
#     def setMessage(self,message):
#         self.message=message
#     def setData(self,data):
#         self.data = data;
# if __name__ == '__main__':
#     app.run(host='0.0.0.0',port=5000)
# # if __name__ == '__main__':
# #     try:
# #         meituan = Meituan()
# #         result = meituan.send_code('15856902498')
# #         print(f'本次登录的最终结果为{result}')
# #
# #
# #         # result = meituan.login_by_password('wmglls2992357','D07921');
# #         # print(f'本次登录的最终结果为{result}')
# #         # if result == None:
# #         #     print(f'获取到的cookie信息为{meituan.getCookie()}')
# #
# #         # result = meituan.login_by_code('15856902499','792886');
# #         # print(f'本次登录的最终结果为{result}')
# #         # if result == None:
# #         #     print(f'获取到的cookie信息为{meituan.getCookie()}')
# #         sleep(2)
# #         meituan.quit();
# #     except Exception as e:
# #         meituan.quit();
#
